package com.yunkeji.api.common.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yunkeji.api.common.bo.VersionCheck;
import com.yunkeji.api.common.service.SysConfigService;
import com.yunkeji.api.common.service.SysParamService;
import com.yunkeji.api.common.vo.SysConfigVo;
import com.yunkeji.common.constant.SysConfigConsts;
import com.yunkeji.common.enums.app.EnableEnum;
import com.yunkeji.common.utils.ServletUtils;
import com.yunkeji.dao.domain.SysCallSnapConfig;
import com.yunkeji.dao.domain.SysVersion;
import com.yunkeji.dao.mapper.SysCityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 基础配置
 *
 * @author Lion Li
 */
@Service
public class SysConfigServiceImpl implements SysConfigService {
    @Autowired SysParamService sysParamService;
    @Autowired SysCityMapper sysCityMapper;

    @Override
    public SysConfigVo configList() {

        String withDrawRate = sysParamService.selectConfigByKey(SysConfigConsts.提现比例);
        String withdrawFeeRate = sysParamService.selectConfigByKey(SysConfigConsts.提现手续费);
        String ossBaseUrl = sysParamService.selectConfigByKey(SysConfigConsts.文件存储基础路径);
        String kmsgUrl = sysParamService.selectConfigByKey(SysConfigConsts.消息系统基础路径);
        String msgFree = sysParamService.selectConfigByKey(SysConfigConsts.文字消息费用);

        List<SysCallSnapConfig> sysCallSnapConfigs =
                SysCallSnapConfig.builder()
                        .build()
                        .selectList(
                                Wrappers.lambdaQuery(SysCallSnapConfig.class)
                                        .eq(
                                                SysCallSnapConfig::getStatus,
                                                EnableEnum.YES.getCode()));
        List<Integer> snapTimes =
                sysCallSnapConfigs.stream()
                        .map(SysCallSnapConfig::getSnapTime)
                        .collect(Collectors.toList());
        //        List<SysCity> sysCities =
        //                sysCityMapper.selectList(
        //                        Wrappers.lambdaQuery(SysCity.class)
        //                                .select(
        //                                        SysCity::getName,
        //                                        SysCity::getId,
        //                                        SysCity::getPid,
        //                                        SysCity::getLevel)
        //                                .le(SysCity::getLevel, 2));

        //        List<CityConfig> cityConfigs = BeanUtil.copyToList(sysCities, CityConfig.class);
        //        List<CityConfig> collect =
        //                cityConfigs.stream()
        //                        .filter(item -> item.getLevel().equals(1))
        //                        .collect(Collectors.toList());
        //
        //        Map<Long, List<CityConfig>> cityInfo =
        //                cityConfigs.stream().collect(Collectors.groupingBy(CityConfig::getPid));

        //        collect.forEach(
        //                item -> {
        //                    if (CollectionUtil.isNotEmpty(cityInfo.get(item.getId()))) {
        //                        item.setSub(cityInfo.get(item.getId()));
        //                    }
        //                });
        return SysConfigVo.builder()
                .msgFree(new BigDecimal(msgFree))
                .snapTimes(snapTimes)
                //    .cityConfig(collect)
                .withDrawRate(withDrawRate)
                .withDrawFreeRate(withdrawFeeRate)
                .ossBaseUrl(ossBaseUrl)
                .kmsgBaseUrl(kmsgUrl)
                .build();
    }

    /**
     * 版本更新检测
     *
     * @author 老张
     */
    @Override
    public SysVersion
    updateCheck(VersionCheck check) {
        SysVersion latest =
                SysVersion.builder()
                        .build()
                        .selectOne(
                                Wrappers.lambdaQuery(SysVersion.class)
                                        .eq(SysVersion::getValidFlag, EnableEnum.YES.getCode())
                                        .eq(SysVersion::getBranch, Integer.valueOf(ServletUtils.getRequest().getHeader("branch")))
                                        .gt(
                                                SysVersion::getVersionIndex,
                                                check.getCurrentVersionIndex())
                                        // .eq(SysVersion::getValidFlag, EnableEnum.YES.getCode())
                                        .orderByDesc(SysVersion::getVersionIndex)
                                        .last(" limit 1"));
        if (latest == null) {
            // 无更新
            return latest;
        }
        String ossBaseUrl = sysParamService.selectConfigByKey(SysConfigConsts.文件存储基础路径);
        latest.setVersionDownloadUrl(ossBaseUrl + latest.getVersionDownloadUrl());
        if (latest.getForceUpdate().equals(EnableEnum.NO.getCode())) {
            // 最新的不为强制更新,找中间是否有强制更新的
            SysVersion sysVersion =
                    SysVersion.builder()
                            .build()
                            .selectOne(
                                    Wrappers.lambdaQuery(SysVersion.class)
                                            .gt(
                                                    SysVersion::getVersionIndex,
                                                    check.getCurrentVersionIndex())
                                            .eq(
                                                    SysVersion::getForceUpdate,
                                                    EnableEnum.YES.getCode())
                                            .eq(SysVersion::getValidFlag, EnableEnum.YES.getCode())
                                            .orderByDesc(SysVersion::getVersionIndex)
                                            .last(" limit 1"));
            if (sysVersion != null) {
                latest.setForceUpdate(EnableEnum.YES.getCode());
            }
        }
        return latest;
    }
}
